/*
========================================================================
 Name        : Test9AppUi.cpp
 Author      : 
 Copyright   : Your copyright notice
 Description : 
========================================================================
*/
// [[[ begin generated region: do not modify [Generated System Includes]
#include <eikmenub.h>
#include <akncontext.h>
#include <akntitle.h>
#include <aknnavide.h>
#include <aknnavi.h>
#include <akntabgrp.h>
#include <Test9.rsg>
// ]]] end generated region [Generated System Includes]

// [[[ begin generated region: do not modify [Generated User Includes]
#include "Test9AppUi.h"
#include "Test9.hrh"
#include "Test9Container.hrh"
#include "Test9ListBox.hrh"
#include "Test9Container2.hrh"
#include "Test9Application.h"
#include "Test9ContainerView.h"
#include "Test9ListBoxView.h"
#include "Test9Container2View.h"
// ]]] end generated region [Generated User Includes]
#include "Settings.h"
// [[[ begin generated region: do not modify [Generated Constants]
// ]]] end generated region [Generated Constants]

/**
 * Construct the CTest9AppUi instance
 */ 
CTest9AppUi::CTest9AppUi()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	iNaviDecorator_ = NULL;
	// ]]] end generated region [Generated Contents]
	
	}

/** 
 * The appui's destructor removes the container from the control
 * stack and destroys it.
 */
CTest9AppUi::~CTest9AppUi()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	if ( iNaviDecorator_ != NULL )
		{
		delete iNaviDecorator_;
		iNaviDecorator_ = NULL;
		}
	// ]]] end generated region [Generated Contents]
	
	}

// [[[ begin generated function: do not modify
void CTest9AppUi::InitializeContainersL()
	{
	CAknNavigationControlContainer* naviPane = ( CAknNavigationControlContainer* )
		StatusPane()->ControlL( TUid::Uid( EEikStatusPaneUidNavi ) );
	iNaviDecorator_ = naviPane->ResourceDecorator();
	if ( iNaviDecorator_ != NULL )
		{
		iNaviTabs1 = ( CAknTabGroup* ) iNaviDecorator_->DecoratedControl();
	
		}
				
	iTest9ContainerView = CTest9ContainerView::NewL();
	AddViewL( iTest9ContainerView );
	SetDefaultViewL( *iTest9ContainerView );
	SetActiveTabByViewId( iTest9ContainerView->Id() );
	iTest9ListBoxView = CTest9ListBoxView::NewL();
	AddViewL( iTest9ListBoxView );
	iTest9Container2View = CTest9Container2View::NewL();
	AddViewL( iTest9Container2View );
	}
// ]]] end generated function

/**
 * Handle a command for this appui (override)
 * @param aCommand command id to be handled
 */
void CTest9AppUi::HandleCommandL( TInt aCommand )
	{
	// [[[ begin generated region: do not modify [Generated Code]
	TBool commandHandled = EFalse;
	switch ( aCommand )
		{ // code to dispatch to the AppUi's menu and CBA commands is generated here
		default:
			break;
		}
	
		
	if ( !commandHandled ) 
		{
		if ( aCommand == EAknSoftkeyExit || aCommand == EEikCmdExit )
			{
			CSettings::theInstance()->StoreL();
			Exit();
			}
		}
	// ]]] end generated region [Generated Code]
	
	TInt active = iNaviTabs1->ActiveTabIndex();
	
	if( !commandHandled )
		{
		if ( aCommand == EAknSoftkeyBack )
			{
			if ( active > 0 )
				{
				active--;
				ActivateLocalViewL( TUid::Uid( iNaviTabs1->TabIdFromIndex( active ) ) );
				}
			}
		
		if ( aCommand == EAknSoftkeyOk )	//Load a specific file (Tab #2).
			{
			ActivateLocalViewL( TUid::Uid( iNaviTabs1->TabIdFromIndex( 2 ) ) );
			}
		}
		
	
	}

/** 
 * Override of the HandleResourceChangeL virtual function
 */
void CTest9AppUi::HandleResourceChangeL( TInt aType )
	{
	CAknViewAppUi::HandleResourceChangeL( aType );
	// [[[ begin generated region: do not modify [Generated Code]
	// ]]] end generated region [Generated Code]
	
	}
				
/** 
 * Override of the HandleKeyEventL virtual function
 * @return EKeyWasConsumed if event was handled, EKeyWasNotConsumed if not
 * @param aKeyEvent 
 * @param aType 
 */
TKeyResponse CTest9AppUi::HandleKeyEventL(
		const TKeyEvent& aKeyEvent,
		TEventCode aType )
	{
	// The inherited HandleKeyEventL is private and cannot be called
	// [[[ begin generated region: do not modify [Generated Contents]
	TVwsViewId activeViewId;
	if ( GetActiveViewId( activeViewId ) == KErrNone 
		&& iNaviTabs1->TabIndexFromId( activeViewId.iViewUid.iUid ) 
			== KErrNotFound )
		{
		return EKeyWasNotConsumed;
		}
	
	TInt active = iNaviTabs1->ActiveTabIndex();
	TInt count = iNaviTabs1->TabCount();
	
/*
	switch ( aKeyEvent.iCode )
		{
		case EKeyLeftArrow:
			if ( active > 0 )
				{
				active--;
				ActivateLocalViewL( TUid::Uid( iNaviTabs1->TabIdFromIndex( active ) ) );
				return EKeyWasConsumed;
				}
			break;
		case EKeyRightArrow:
			if ( active + 1 < count )
				{
				active++;
				ActivateLocalViewL( TUid::Uid( iNaviTabs1->TabIdFromIndex( active ) ) );
				return EKeyWasConsumed;
				}
			break;
		default:
			return EKeyWasNotConsumed;
		}
*/
	
				
	// ]]] end generated region [Generated Contents]
	
	if(aKeyEvent.iScanCode == 53 && aType == EEventKeyUp)	//Load All Files (Tab #1)
		{
			ActivateLocalViewL( TUid::Uid( iNaviTabs1->TabIdFromIndex( 1 ) ) );
			return EKeyWasConsumed;		
		}
	
	return EKeyWasNotConsumed;
	}

/** 
 * Override of the HandleViewDeactivation virtual function
 *
 * @param aViewIdToBeDeactivated 
 * @param aNewlyActivatedViewId 
 */
void CTest9AppUi::HandleViewDeactivation( 
		const TVwsViewId& aViewIdToBeDeactivated, 
		const TVwsViewId& aNewlyActivatedViewId )
	{
	CAknViewAppUi::HandleViewDeactivation( 
			aViewIdToBeDeactivated, 
			aNewlyActivatedViewId );
	// [[[ begin generated region: do not modify [Generated Contents]
	if ( aNewlyActivatedViewId.iAppUid == KUidTest9Application )
		{
		SetActiveTabByViewId( aNewlyActivatedViewId.iViewUid );
		}
	// ]]] end generated region [Generated Contents]
	
	}

/**
 * @brief Completes the second phase of Symbian object construction. 
 * Put initialization code that could leave here. 
 */ 
void CTest9AppUi::ConstructL()
	{
	// [[[ begin generated region: do not modify [Generated Contents]
	
	BaseConstructL( EAknEnableSkin ); 
	InitializeContainersL();
	// ]]] end generated region [Generated Contents]
	
	}

// [[[ begin generated function: do not modify
void CTest9AppUi::SetActiveTabByViewId( TUid aViewId )
	{
	if ( iNaviTabs1 != NULL 
		&& iNaviTabs1->TabIndexFromId( aViewId.iUid ) != KErrNotFound )
		{
		iNaviTabs1->SetActiveTabById( aViewId.iUid );
		}
	}
// ]]] end generated function

